﻿using System;
using System.IO;
using System.Linq;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using PIBV;
using MTViewer.ViewModels;

namespace MTViewer
{
    public interface ITouchObject
    {
        double Angle { get; set; }
        string ImagePath { get; set; }
        double ScaleX { get; set; }
        double ScaleY { get; set; }
        double X { get; set; }
        double Y { get; set; }
        double Width { get; set; }
        double Height { get; set; }
        string PdfFileName { get; set; }
        string OriginalImagePath { get;}
        string DgnFileName { get; set;}
        event EventHandler<LoadDrawingEventArgs> OnCreateControl;
    }

    public static class TouchObjectFactory
    {

        public static Canvas CanvasInstance;

        public static void RemoveTouchObjectsFromCanvas()
        {
            Canvas c = CanvasInstance;
            for (int i = c.Children.Count-1; i >= 0; i--)
            {
                ITouchObject obj = c.Children[i] as ITouchObject;
                if (obj != null)
                {
                    c.Children.Remove(c.Children[i]);
                }
            }
            GC.Collect();
            GC.Collect();
            GC.Collect();
        }

        public static void RemoveTouchObjectFromCanvas(ITouchObject obj)
        {
            Canvas c = CanvasInstance;

            if (obj != null && (obj as UIElement)!=null)
            {
                c.Children.Remove(obj as UIElement);
            }

            GC.Collect();
            GC.Collect();
            GC.Collect();
        }

        public static ITouchObject GetTopTouchObjectFromCanvas()
        {
            Canvas c = CanvasInstance;
            int zIndexMax = -1;
            ITouchObject result = null;

            for (int i = c.Children.Count - 1; i >= 0; i--)
            {
                ITouchObject obj = c.Children[i] as ITouchObject;
                if (obj != null)
                {
                    int z = (int)c.Children[i].GetValue(Canvas.ZIndexProperty);
                    if (z > zIndexMax)
                    {
                        zIndexMax = z;
                        result = c.Children[i] as ITouchObject;
                    }
                }
            }
            return result;
        }

        public static string[] GetPdfFileNamesOnCanvas()
        {
            
            Canvas c = CanvasInstance;
            string[] result = null;

            if (c == null || c.Children.Count == 0) return null;

            result = new string[c.Children.Count];

            for (int i = 0; i < c.Children.Count; i++)
            {
                ITouchObject obj = c.Children[i] as MTViewer.Picture;

                if (obj != null)
                    result[i] = obj.PdfFileName;

            }

            return result.Where(x => x != null).ToArray();
        }

        public static T GetControl<T>() where T:class,new()
        {
            Canvas c = CanvasInstance;
            T result = null;
            for (int i = c.Children.Count - 1; i >= 0; i--)
            {
                T obj = c.Children[i] as T;
                if (obj != null)
                {
                    result = obj;
                }
            }
            return result;
        }

        public static UserControl GetDbPanel()
        {
            Canvas c = CanvasInstance;
            UserControl result = null;
            
            for (int i = c.Children.Count - 1; i >= 0; i--)
            {
                DbPanel  obj = c.Children[i] as DbPanel;
                if (obj != null)
                {
                    result = obj;
                }
            }

            return result;
        }

        public static ITouchObject CreateTouchObject(string path)
        {
            ITouchObject result = null;
            switch (Path.GetExtension(path))
            {
                case ".tif": 
                    //create drawing from image
                    result = new Picture() { ImagePath = path, OriginalImagePath = path }; 
                    break;
                case ".png": 
                    //create drawing from image
                    result = new Picture() { ImagePath = path, OriginalImagePath = path }; 
                    break;
                case ".cap":
                    //create CAP Matrix control
                    result = new ctrlCapMatrix(){ ImagePath = path, OriginalImagePath = path };
                    break;
                case ".bll":
                    ShowTagsViewModel vm = new ShowTagsViewModel();
                    result = new ShowTagView(vm){ ImagePath = path, OriginalImagePath = path };
                    break;

                case ".cef":
                    //create CauseEffect control
                    result = new ctrlCauseEffect(){ ImagePath = path, OriginalImagePath = path };
                    break;

                default:
                    throw new ArgumentException("create touch object","path");
            }

            return result;
        }

        public static bool IsDrawing(ITouchObject p)
        {
            bool result = false;

            if (p != null && Path.GetExtension(p.ImagePath)==".tif")
            {
                result = true;
            }

            return result;
        }

    }
}
